﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DAL;
using System.Text;
using System.IO;
using DevExpress.Web.ASPxPager;
using System.Data.SqlClient;

public partial class ShowReports : System.Web.UI.Page
{
    DataClassesDataContext dc = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack && !IsCallback)
        {
            //判断用户是否已经登录
            try
            {
                if (!CMSUser.Current.IsLogin)
                {
                    Response.Redirect("~/Login.aspx");
                }
            }
            catch (Exception)
            {
                Response.Redirect("~/Login.aspx");
            }
            Session["sqlstr"] = null;
            Session["sqlParams"] = null;
            GvBind();
        }
        //DDataBind();
    }

    // 获取批文号
    //protected void cmbApprovalNum_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    //{
    //    FillAppNumCombo(e.Parameter);
    //}
    //protected void FillAppNumCombo(string str)
    //{
    //    if (string.IsNullOrEmpty(str)) return;
    //    SDSAppNum.SelectParameters[0].DefaultValue = str;
    //    cmbAppNum.DataBind();
    //}

    DataTable table;
    public DataTable Table
    {
        get
        {
            if (Session["sqlstr"] != null && Session["sqlParams"] != null)
            {
                table = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(Session["sqlstr"].ToString(), Session["sqlParams"] as SqlParameter[]);
            }
            return table;
        }
    }
    void DDataBind()
    {
        if (Session["sqlstr"] != null && Session["sqlParams"] != null)
        {
            gvShowReports.DataSource = Table;
            gvShowReports.DataBind();
        }
    }

    //合并第一列相同的项目类型
    int row = 0;
    protected void gvShowReports_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int rowindex = e.Row.RowIndex;
        if (rowindex - 1 < 0) return;
        if (e.Row.Cells[0].Text == gvShowReports.Rows[rowindex - 1].Cells[0].Text)
        {
            if (gvShowReports.Rows[row].Cells[0].RowSpan == 0) gvShowReports.Rows[row].Cells[0].RowSpan++;
            gvShowReports.Rows[row].Cells[0].RowSpan++;
            e.Row.Cells[0].Visible = false;
        }
        else
        {
            row = rowindex;
        }
    }

    //设置表头格式
    protected void gvShowReports_RowCreated(object sender, GridViewRowEventArgs e)
    {
        switch (e.Row.RowType)
        {
            case DataControlRowType.Header:

                //总表头
                TableCellCollection tcHeader = e.Row.Cells;
                tcHeader.Clear();
                tcHeader.Add(new TableHeaderCell());
                tcHeader[0].Attributes.Add("colspan", "8");
                string d = txtDate.Text.ToString();
                tcHeader[0].Text = "杭州市社区服务业资金补助项目建设情况汇总表</th></tr><tr>";
                tcHeader[0].Font.Size = FontUnit.Parse("25");
                //第一行表头
                tcHeader.Add(new TableHeaderCell());
                tcHeader[1].Attributes.Add("rowspan", "2");
                tcHeader[1].Text = "项目类型";
                tcHeader[1].Font.Name = "微软雅黑";
                tcHeader[1].Font.Bold = false;
                tcHeader.Add(new TableHeaderCell());
                tcHeader[2].Attributes.Add("rowspan", "2");
                tcHeader[2].Text = "项目名称";
                tcHeader[2].Font.Name = "微软雅黑";
                tcHeader[2].Font.Bold = false;
                tcHeader.Add(new TableHeaderCell());
                tcHeader[3].Attributes.Add("rowspan", "2");
                tcHeader[3].Text = "项目实施单位";
                tcHeader[3].Font.Name = "微软雅黑";
                tcHeader[3].Font.Bold = false;
                tcHeader.Add(new TableHeaderCell());
                tcHeader[4].Attributes.Add("rowspan", "2");
                tcHeader[4].Text = "项目基本信息";
                tcHeader[4].Font.Name = "微软雅黑";
                tcHeader[4].Font.Bold = false;
                tcHeader.Add(new TableHeaderCell());
                tcHeader[5].Attributes.Add("colspan", "3");
                tcHeader[5].Text = "项目经费情况（万元）";
                tcHeader[5].Font.Name = "微软雅黑";
                tcHeader[5].Font.Bold = false;
                tcHeader.Add(new TableHeaderCell());
                tcHeader[6].Attributes.Add("rowspan", "2");
                tcHeader[6].Text = "项目完成情况 </th></tr><tr>";
                tcHeader[6].Font.Name = "微软雅黑";
                tcHeader[6].Font.Bold = false;
                tcHeader.Add(new TableHeaderCell());
                //第二行表头

                tcHeader[7].Text = "市级补助情况";
                tcHeader[7].Font.Name = "微软雅黑";
                tcHeader[7].Font.Bold = false;
                tcHeader.Add(new TableHeaderCell());
                tcHeader[8].Text = "区（街道）资金配套情况";
                tcHeader[8].Font.Name = "微软雅黑";
                tcHeader[8].Font.Bold = false;
                tcHeader.Add(new TableHeaderCell());
                tcHeader[9].Text = "总投入经费";
                tcHeader[9].Font.Name = "微软雅黑";
                tcHeader[9].Font.Bold = false;
                break;
        }
    }

    //导出到Excel
    protected void btnExport_Click(object sender, EventArgs e)
    {
        DDataBind();
        string filename = txtDate.Text.Trim() + "杭州市" + cmbDis.Text + "社区服务业资金补助项目建设情况汇总表.xls";
        ExportExcel(filename, "application/ms-excel");
    }
    /// <summary>
    /// 导出Excel方法
    /// </summary>
    /// <param name="filename">文件名称</param>
    /// <param name="filentype">文件类型</param>
    private void ExportExcel(string filename, string filentype)
    {
        GridView gv = Session["gv"] as GridView;
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        Response.ContentEncoding = Encoding.UTF8;
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, Encoding.UTF8).ToString());
        Response.ContentType = filentype;
        gvShowReports.Page.EnableViewState = true;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        gvShowReports.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
    }

    //按条件查询
    protected void CBPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
        string SqlFormat = @" SELECT TypeInfo.Type_Name,
                                    ProjectDetails.Project_Name,
                                    ProjectDetails.Project_Development,
                                     ProjectDetails.Project_BasicInfo,
                                    ProjectDetails.Project_Address,
                                    ProjectDetails.Project_Scope,
                                    ProjectDetails.ServeObiect,
                                    ProjectDetails.ServerPersontime,
                                    ProjectDetails.ContactWay,
                                    ProjectDetails.other,
                                    ProjectDetails.Project_CitySubsidy,
                                    ProjectDetails.Project_DistrictFund,
                                    ProjectDetails.Project_FundsTotal,
                                    ProjectDetails.Community_ID,
                                    ProjectDetails.ApprovalNumber,
                                    ProjectDetails.FinishDate,
                                    Approal.Approal_Date,
                                    Project_Performance
                                    FROM ProjectDetails
                                    INNER JOIN TypeInfo
                                    ON ProjectDetails.Type_ID=TypeInfo.Type_ID
                                    INNER JOIN Approal
                                    ON Projectdetails.ApprovalNumber=Approal.ID
                                    WHERE {0} 1=1
                                    ORDER BY ProjectDetails.Type_ID
                                ";
        StringBuilder strb = new StringBuilder();
        List<SqlParameter> Params = new List<SqlParameter>();
        if (!string.IsNullOrEmpty(cmbDis.Text.Trim()))
        {
            strb.Append("ProjectDetails.Community_ID like ''+@C_ID+'%'  AND ");
            Params.Add(new SqlParameter("@C_ID", cmbDis.Value.ToString()));
        }
        if (!string.IsNullOrEmpty(cmbType.Text.Trim()))
        {
            strb.Append("ProjectDetails.Type_ID = @T_ID  AND ");
            Params.Add(new SqlParameter("@T_ID", Convert.ToInt32(cmbType.Value.ToString())));
        }
        if (!string.IsNullOrEmpty(cmbAppNum.Text.Trim()))
        {
            strb.Append("Approal.Approal_ID=@AppNum AND ");
            Params.Add(new SqlParameter("@AppNum", cmbAppNum.Value.ToString()));
        }
        if (!string.IsNullOrEmpty(txtDate.Text.Trim()))
        {
            strb.Append("Approal.Approal_Date like ''+@F_D+'%' AND ");
            Params.Add(new SqlParameter("@F_D", txtDate.Text.Trim()));
        }

        Session["sqlstr"] = string.Format(SqlFormat, strb.ToString());
        Session["sqlParams"] = Params.ToArray();
        gvShowReports.DataSource = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(string.Format(SqlFormat, strb.ToString()), Params.ToArray());
        gvShowReports.DataBind();
    }

    //绑定全部信息
    void GvBind()
    {
        string Sql = @" SELECT TypeInfo.Type_Name,
                                    ProjectDetails.Project_Name,
                                    ProjectDetails.Project_Development,
                                    ProjectDetails.Project_BasicInfo,
                                    ProjectDetails.Project_Address,
                                    ProjectDetails.Project_Scope,
                                    ProjectDetails.ServeObiect,
                                    ProjectDetails.ServerPersontime,
                                    ProjectDetails.ContactWay,
                                    ProjectDetails.other,
                                    ProjectDetails.Project_CitySubsidy,
                                    ProjectDetails.Project_DistrictFund,
                                    ProjectDetails.Project_FundsTotal,
                                    ProjectDetails.Community_ID,
                                    ProjectDetails.ApprovalNumber,
                                    ProjectDetails.FinishDate,
                                    Project_Performance
                                    FROM ProjectDetails
                                    INNER JOIN TypeInfo
                                    ON ProjectDetails.Type_ID=TypeInfo.Type_ID
                                    ORDER BY ProjectDetails.Type_ID
                                ";
        gvShowReports.DataSource = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(string.Format(Sql));
        gvShowReports.DataBind();
    }


    //获取基本信息
    public string GetAllBasicInfo(string bdes, string address, string scope, string serveObiect, string serverPersontime, string contactWay, string other)
    {
        return "基本描述：" + bdes + "</br>地址：" + address + "</br>规模：" + scope + "</br>服务对象：" + serveObiect + "</br>服务人次：" + serverPersontime + "</br>联系方式：" + contactWay + "</br>其他：" + other;
    }
}